package com.wty.order.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wty.commons.result.DataGrid;
import com.wty.commons.result.Result;
import com.wty.order.pojo.Order;
import com.wty.order.service.OrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wty
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /*添加订单*/
    @ApiOperation("添加订单")
    @ApiImplicitParam(name = "order",required = true,dataType = "Order",dataTypeClass = Order.class)
    @PostMapping
    public Result<Order> addOrder(@RequestBody Order order){
        order.setCreateTime(LocalDateTime.now());
        order.setStatus(0);
        orderService.save(order);
        return Result.OK(order);
    }

    /*修改订单*/
    @ApiOperation("修改订单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required = true,dataType = "Long",dataTypeClass = Long.class),
        @ApiImplicitParam(name = "order",required = true,dataType = "Order",dataTypeClass = Order.class)
    })
    @PutMapping("{id}")
    public Result<Order> updateOrder(@PathVariable Long id,@RequestBody Order order){
        order.setOrderId(id);
        orderService.updateById(order);
        Order orderData= orderService.getById(id);
        return Result.OK(orderData);
    }

    /*删除订单*/
    @ApiOperation("删除订单")
    @ApiImplicitParam(name = "id",required = true,dataType = "Long",dataTypeClass = Long.class)
    @DeleteMapping("{id}")
    public Result<Boolean> deleteOrder(@PathVariable Long id){
        boolean result = orderService.deleteById(id);
        return Result.OK(result);
    }

    /*批量删除订单*/
    @ApiOperation("批量删除订单")
    @DeleteMapping
    public Result<Boolean> deleteOrders(@RequestParam List<Long> ids){
//        System.out.println(ids.toString());
        boolean result = orderService.deleteBatchById(ids);
        return Result.OK(result);
    }

    /*查询订单 分页*/
    @ApiOperation("分页查询订单")
    @GetMapping("{current}/{size}")
    public Result<DataGrid<Order>> selectList(@PathVariable Long current,@PathVariable Long size){
        IPage<Order> page = orderService.page(new Page<>(current, size));
        DataGrid<Order> dataGrid = new DataGrid<>();
        dataGrid.setTotal(page.getTotal());
        dataGrid.setRows(page.getRecords());
        return Result.OK(dataGrid);
    }

    /*查询订单 条件 分页*/
    @ApiOperation("查询订单 条件 分页")
    @PostMapping("{current}/{size}")
    public Result<DataGrid<Order>> selectList(@PathVariable Long current,@PathVariable Long size,@RequestBody Order order){
        Wrapper<Order> queryWrapper = new QueryWrapper<>(order);
        IPage<Order> page = orderService.page(new Page<>(current, size),queryWrapper);
        DataGrid<Order> dataGrid = new DataGrid<>();
        dataGrid.setTotal(page.getTotal());
        dataGrid.setRows(page.getRecords());
        return Result.OK(dataGrid);
    }
}

